﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using KPIS.DBM;
using KPIS.GERP.GFMS.MODEL;
using KPIS.GERP.GFMS.IDAL;
namespace KPIS.GERP.GFMS.DAL
{
    public class D_FIM_SystemControls : IDAL_FIM_SystemControls
    {
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, ConfigurationSettings.AppSettings["ConnectionString"].ToString());
        ConvertNullable cn = new ConvertNullable();
        //

        public M_FIM_SystemControls GetInfo()
        {
            string strSQL;
            DataRowCollection drc = null;
            M_FIM_SystemControls info = new M_FIM_SystemControls();

            try
            {
                dbManager.Open();

                strSQL = "SELECT sc.use_decimal, sc.default_bgm_year, sc.default_vat_rate, sc.default_tax_rate"
                    + ", sc.default_currency_seq, sc.finance_job_site_seq"

                    + ", sc.created_by, sc.created_when, sc.updated_by, sc.updated_when"
                    + " FROM fim_system_controls sc";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.use_decimal = cn.ConvertNullInt(drc[0]["use_decimal"]);
                    info.default_bgm_year = cn.ConvertNullInt(drc[0]["default_bgm_year"]);
                    info.default_vat_rate = cn.ConvertNullDouble(drc[0]["default_vat_rate"]);
                    info.default_tax_rate = cn.ConvertNullDouble(drc[0]["default_tax_rate"]);
                    info.default_currency_seq = cn.ConvertNullInt(drc[0]["default_currency_seq"]);
                    info.finance_job_site_seq = cn.ConvertNullInt(drc[0]["finance_job_site_seq"]);

                    info.created_by = cn.ConvertNullInt(drc[0]["created_by"]);
                    info.created_when = cn.ConvertNullDateTime(drc[0]["created_when"]);
                    info.updated_by = cn.ConvertNullInt(drc[0]["updated_by"]);
                    info.updated_when = cn.ConvertNullDateTime(drc[0]["updated_when"]);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }

        public string GetSystemName()
        {
            string strSQL;
            DataTable dtb = null;

            try
            {
                dbManager.Open();

                strSQL = "SELECT *"
                    + " FROM sys_system_controls";

                dtb = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];
                if (dtb.Rows.Count > 0)
                {
                    return dtb.Rows[0]["system_name"].ToString();
                }

                return "";
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(M_FIM_SystemControls info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(8);

                dbManager.AddParameters(0, "@use_decimal", info.use_decimal);
                dbManager.AddParameters(1, "@default_bgm_year", info.default_bgm_year);
                dbManager.AddParameters(2, "@default_vat_rate", info.default_vat_rate);
                dbManager.AddParameters(3, "@default_tax_rate", info.default_tax_rate);
                dbManager.AddParameters(4, "@default_currency_seq", info.default_currency_seq);
                dbManager.AddParameters(5, "@finance_job_site_seq", info.finance_job_site_seq);

                dbManager.AddParameters(6, "@updated_by", info.updated_by);
                dbManager.AddParameters(7, "@updated_when", info.updated_when);

                strSQL = "UPDATE fim_system_controls"
                    + " SET use_decimal = @use_decimal"
                    + ", default_bgm_year = @default_bgm_year"
                    + ", default_vat_rate = @default_vat_rate"
                    + ", default_tax_rate = @default_tax_rate"
                    + ", default_currency_seq = @default_currency_seq"
                    + ", finance_job_site_seq = @finance_job_site_seq"
                    + ", updated_by = @Updated_by"
                    + ", updated_when = @Updated_when";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}